package com.yzdmw.toutiao.login.ui;

import androidx.annotation.Nullable;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import android.util.Patterns;

import com.yzdmw.toutiao.login.data.LoginRepository;
import com.yzdmw.toutiao.R;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class LoginViewModel extends ViewModel {
    // 状态存储
    private final LoginRepository loginRepository;
    public LoginViewModel() {
        this.loginRepository = LoginRepository.getInstance();
    }

    // 登陆表单状态
    static class LoginFormState {
        @Nullable
        private final Integer usernameError;
        @Nullable
        private final Integer passwordError;
        private final boolean isDataValid;

        LoginFormState(@Nullable Integer usernameError, @Nullable Integer passwordError) {
            this.usernameError = usernameError;
            this.passwordError = passwordError;
            this.isDataValid = false;
        }

        LoginFormState(boolean isDataValid) {
            this.usernameError = null;
            this.passwordError = null;
            this.isDataValid = isDataValid;
        }

        @Nullable
        Integer getUsernameError() {
            return usernameError;
        }

        @Nullable
        Integer getPasswordError() {
            return passwordError;
        }

        boolean isDataValid() {
            return isDataValid;
        }
    }
    private final MutableLiveData<LoginFormState> loginFormState = new MutableLiveData<>();
    LiveData<LoginFormState> getLoginFormState() {
        return loginFormState;
    }

    // 登陆结果
    static class LoginResult {
        @Nullable
        private final Integer error;
        LoginResult(@Nullable Integer error) {
            this.error = error;
        }
        @Nullable
        Integer getError() {
            return error;
        }
    }
    private final MutableLiveData<LoginResult> loginResult = new MutableLiveData<>();
    LiveData<LoginResult> getLoginResult() {
        return loginResult;
    }

    public void login(String username, String password) {
        //noinspection ResultOfMethodCallIgnored
        loginRepository.login(username, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()) //LiveData的更新需要在ui线程
                .subscribe(result -> {
                    if (result.success) {
                        loginResult.setValue(new LoginResult(null));
                    } else {
                        loginResult.setValue(new LoginResult(result.code));
                    }
                    loginResult.setValue(null);
                });
    }
    public void regist(String username, String password) {
        //noinspection ResultOfMethodCallIgnored
        loginRepository.regist(username, password)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()) //LiveData的更新需要在ui线程
                .subscribe(result -> {
                    if (result.success) {
                        loginResult.setValue(new LoginResult(null));
                    } else {
                        loginResult.setValue(new LoginResult(result.code));
                    }
                    loginResult.setValue(null);
                });
    }

    public void loginDataChanged(String username, String password) {
        if (isUserNameInvalid(username)) {
            loginFormState.setValue(new LoginFormState(R.string.invalid_username, null));
        } else if (isPasswordInvalid(password)) {
            loginFormState.setValue(new LoginFormState(null, R.string.invalid_password));
        } else {
            loginFormState.setValue(new LoginFormState(true));
        }
    }
    public void registDataChanged(String username, String password, String confirm_password) {
        if (isUserNameInvalid(username)) {
            loginFormState.setValue(new LoginFormState(R.string.invalid_username, null));
        } else if (isPasswordInvalid(password)) {
            loginFormState.setValue(new LoginFormState(null, R.string.invalid_password));
        } else if (!password.equals(confirm_password)) {
            loginFormState.setValue(new LoginFormState(null, R.string.invalid_confirm_password));
        } else {
            loginFormState.setValue(new LoginFormState(true));
        }
    }

    // A placeholder username validation check
    private boolean isUserNameInvalid(String username) {
        if (username == null) {
            return true;
        }
        if (username.contains("@")) {
            return !Patterns.EMAIL_ADDRESS.matcher(username).matches();
        } else {
            return username.trim().isEmpty();
        }
    }

    // A placeholder password validation check
    private boolean isPasswordInvalid(String password) {
        return password == null || password.trim().length() <= 5;
    }
}